/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.navigation.route;

import android.content.Intent;
import android.location.Location;
import android.os.Parcelable;
import android.util.Log;
import ch.bfh.aeneas.exchange.intent.AeneasIntent;
import ch.bfh.aeneas.exchange.intent.MalformedIntentException;
import ch.bfh.aeneas.exchange.data.Node;
import ch.bfh.aeneas.navigation.app.NavigationService;
import ch.bfh.aeneas.exchange.Scope;

/**
 * This is the backing service of RouteMe. It requests routes from both, remote 
 * and local providers.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class RouteService extends NavigationService {

    private String LOG_TAG = "RouteService";
    
    public int routeMeLocal(Node from, Node to) {
        //Location loc = DataConverter.node2Location(n);
        Parcelable pTo = (Parcelable) to;
        
        AeneasIntent i = getBroadcastIntent();
        i.putExtra("scope", Scope.LOCAL);
        i.putExtra("type", "route");
        i.putExtra("to", pTo);
        //i.putExtra("range", 0);
        try {
            broadcast(i, 10000);
            return i.getRequestId();
        } catch (MalformedIntentException ex) {
            Log.w(LOG_TAG, "Malformed Intent in routeMe of RouteService!");
            return -1;
        }
    }
    
    public int routeMeRemote(Node from, Node to) {
        //Location loc = DataConverter.node2Location(n);
        Parcelable pTo = (Parcelable) to;
        Parcelable pFrom = (Parcelable) from;
        
        AeneasIntent i = getBroadcastIntent();
        i.putExtra("scope", Scope.REMOTE);
        i.putExtra("type", "route");
        i.putExtra("from", pFrom);
        i.putExtra("to", pTo);
        //i.putExtra("range", 0);
        try {
            broadcast(i, 10000);
            return i.getRequestId();
        } catch (MalformedIntentException ex) {
            Log.w(LOG_TAG, "Malformed Intent in routeMe of RouteService!");
            return -1;
        }
    }
    
    private int sendRequest(Location from, Location to, int duration) {
        AeneasIntent i = getBroadcastIntent();
        i.putExtra("type", "route");
        i.putExtra("from", from);
        i.putExtra("to", to);
        
        try {
            broadcast(i, duration);
            return i.getRequestId();
        } catch (MalformedIntentException ex) {
            Log.w(LOG_TAG, "Malformed Intent in sendRequest of RouteService!");
            return -1;
        }
    }
    
    private void sendRequest(Location from, Location to) {
        sendRequest(from, to, 10000);
    }
    
    /**
     * {@inheritDoc }
     */
    protected void onAnswer(Intent intent) {
        Log.i(LOG_TAG, "parsing answer");
        informOnResult(intent);
    }
}
